<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>管理员列表</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<table id="permList" class="permList" lay-filter="permList"></table>
<script type="text/html" id="barEdit">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script th:src="@{/src/layui.js}"></script>
<script>
    layui.use(['jquery','layer','laypage','laytpl','table'],function () {
        var $ = layui.jquery,
            layer = layui.layer,
            laypage = layui.laypage,
            laytpl = layui.laytpl,
            table = layui.table;
        table.render({
            elem:'#permList',
            url:'/admin/list',
            title:'小区列表',
            page:true,
            limit:10,
            limits:[10,20],
            cols:[
                [
                    {type:'checkbox'},
                    {title:'ID',field:'id',width:50},
                    {title:'账户',field:'account'},
                    {title:'手机号码',field:'phone'},
                    {title:'姓名',field:'adminName'},
                    {title:'身份证号码',field:'adminCard'},
                    {title:'角色',field:'role'},
                    {title:'邮箱',field:'email'},
                    {title:'创建时间',field:'createTime'},
                    {title:'操作',templet:'#barEdit'}
                ]
            ],initSort: {
                field: 'createTime' //排序字段，对应 cols 设定的各字段名
                , type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
            },parseData:function(result){
                return {
                    code:result.code==200?0:result.code,
                    msg:result.message,
                    count:result.data.total,
                    data:result.data.list
                }
            }
        });
        //监听表格的行工具事件
        table.on('tool(permList)',function (row) {
            var data = row.data;
            if (data.role == '超级管理员'){
                layer.msg("超级管理员不允许操作！");
            } else {
                if(row.event === 'edit'){
                    layer.open({
                        type:2,
                        title:'编辑权限',
                        area:['300px','400px'],
                        content:'/admin/adminRole?id='+data.id
                    });

                } else if(row.event === 'del'){
                layer.confirm("确定删除吗？",function (index) {
                        $.ajax({
                            url: '/admin/delete',
                            type: 'delete',
                            data: {'id': data.id},
                            dataType: 'json',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.message, {icon: 1}, function () {
                                        location.reload();//删除成功刷新页面
                                    });
                                } else {
                                    layer.msg(result.message, {icon: 5});
                                }
                            }
                        });
                        layer.close(index);
                });
            }
            }

        })




    })
</script>

</body>
</html>